<!--
 * @Author: yansheng 610363806@qq.com
 * @Date: 2024-07-05 00:06:44
 * @LastEditors: yansheng 610363806@qq.com
 * @LastEditTime: 2024-07-05 00:57:28
 * @FilePath: \cell-admin\src\pages\user\adminPassword.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <a-drawer
    :title="'修改密码'"
    placement="right"
    :closable="true"
      :maskClosable='false'
    :keyboard='false'
    width="800"
    v-model:open="visiblePassword"
  >
    <div>
      <a-form
        ref="formRef"
        :model="formData"
        :label-col="{ span: 4 }"
      >
        <a-form-item
          label="新密码"
          name="password"
          :rules="[{ required: false, validator: validatePass, trigger: 'blur' }]"
        >
          <a-input v-model:value="formData.password"></a-input>
        </a-form-item>

        <a-form-item
          label="再次确认密码"
          name="password2"
          :rules="[{ required: false, validator: validatePass2, trigger: 'blur' }]"
        >
          <a-input v-model:value="formData.password2"></a-input>
        </a-form-item>

        <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button
            type="primary"
            @click="handlePasswordOk"
          >
            确认
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </a-drawer>
</template>
<script setup>
  import { adminList, adminDel, adminUpdate, adminAdd } from '@/api/index.js'
  import { message } from 'ant-design-vue'

  import { ref, reactive, onMounted, nextTick, toRefs, defineExpose, defineEmits } from 'vue'
  import { useRouter } from 'vue-router'
  const emits = defineEmits(['adminReload'])
  const validatePass = async (_rule, value) => {
    if (value === '') {
      return Promise.reject('请输入密码')
    } else {
      return Promise.resolve()
    }
  }
  const validatePass2 = async (_rule, value) => {
    if (value === '') {
      return Promise.reject('请再次输入密码')
    } else if (value !== formData.value.password) {
      return Promise.reject('两次密码不一致')
    } else {
      return Promise.resolve()
    }
  }
  const formData = ref({
    admin_id: null,
    password: null,
    password2: null
  })
  const formRef = ref()

  const visiblePassword = ref(false)
  const init = (row) => {
    visiblePassword.value = true
    nextTick(() => {
      if (row) {
        ;(formData.value.admin_id = row.admin_id), (formData.value.password = ''), (formData.value.password2 = '')
      }
    })
  }
  const handlePasswordOk = () => {
    formRef.value.validate().then(() => {
      adminUpdate({
        admin_id: formData.value.admin_id,
        password: formData.value.password
      }).then((res) => {
        if(res.errorCode){
        message.error(res.msg||'操作失败')
        return
      }
        if (res.code == 200) {
          emits('adminReload')
          visiblePassword.value = false
        }
      })
    })
  }
  defineExpose({
    init
  })
</script>
